package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyCatalogMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyTagMapper;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.common.query.StrategyDetailQuery;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper strategyContentMapper;
    @Autowired
    private StrategyCatalogMapper strategyCatalogMapper;
    @Autowired
    private StrategyTagMapper strategyTagMapper;

    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo(list);
    }

    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    public StrategyContent getContent(Long id) {
        return strategyContentMapper.selectByPrimaryKey(id);
    }

    public void saveOrUpdate(StrategyDetail entity, String tags) {

        //设置目的地
        Long id = entity.getCatalog().getId();
        StrategyCatalog catalog = strategyCatalogMapper.selectByPrimaryKey(id);
        Long destId = catalog.getDestId();
        Destination dest = new Destination();
        dest.setId(destId);
        entity.setDest(dest);

        //设置摘要,调用第三方AI接口
        entity.setSummary("AI");

        StrategyContent content = entity.getStrategyContent();
        content.setId(entity.getId());

        if (entity.getId() == null) {
            //设置创建时间
            entity.setCreateTime(new Date());
            //保存文章
            strategyDetailMapper.insert(entity);
            //保存内容
            strategyContentMapper.insert(content);
        } else {
            //更新文章
            strategyDetailMapper.updateByPrimaryKey(entity);
            //更新内容
            strategyContentMapper.updateByPrimaryKey(content);
            //同步分类
            StrategyCatalog strategyCatalog = new StrategyCatalog();
            strategyCatalog.setId(entity.getCatalog().getId());//分类id
            strategyCatalog.setDestId(destId);
            strategyCatalogMapper.updateRelation(strategyCatalog);
        }

        //处理标签
        if (tags != null) {
            //删除关系
            strategyTagMapper.deleteRelation(entity.getId());

            String[] ts = tags.split(",");
            for (String tagName : ts) {
                StrategyTag tag = strategyTagMapper.selectByName(tagName);
                if (tag == null) {//如果该标签数据库中不存在
                    tag = new StrategyTag();
                    tag.setName(tagName);
                    strategyTagMapper.insert(tag);//就添加当前标签
                }
                //添加关系
                strategyTagMapper.insertRelation(entity.getId(), tag.getId());
            }
        }

    }

    public void changeState(Long id, Integer state) {
        strategyDetailMapper.updateState(id, state);
    }

    public List<StrategyDetail> listViewnumTop3(Long id) {
        return strategyDetailMapper.selectViewnumTop3(id);
    }

    public List<StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    public void updateStats(StrategyDetail detail) {
        strategyDetailMapper.updateStats(detail);
    }
}
